﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Subsidiary.CouponWrapper
{
    public class EtaoCouponWrapper : ICouponWrapper
    {
        public static readonly string trPattern = @"<tr[^>]*>(.+?)</tr>";
        public static readonly string tdPattern = @"<td>(.+?)</td>";
        public List<CouponItem> PickUp(string html)
        {
            List<CouponItem> coupons = new List<CouponItem>();
            MatchCollection trMatches = Regex.Matches(html, trPattern);
            for (int i = 0; i < trMatches.Count; i++)
            {
                Match trMatch = trMatches[i];
                string trHtml = trMatch.Groups[1].Value;
                MatchCollection tdMatches = Regex.Matches(trHtml, tdPattern);
                string limit = tdMatches[5].Groups[1].Value;
                CouponItem coupon = new CouponItem
                {
                    //1 Shop Link
                    Shop = "VJIA",
                    Link = "http://www.dwz.cn/4Prl1",
                    // Title 放空
                    Title = tdMatches[2].Groups[1].Value,
                    //5 enddate
                    EndDate = "有效期到" + tdMatches[6].Groups[1].Value,
                    //3 number password
                    Number = tdMatches[0].Groups[1].Value,
                    Password = tdMatches[1].Groups[1].Value,
                    //2,4 limit, amount
                    Amount = float.Parse(tdMatches[4].Groups[1].Value.Substring(1)),
                    Comment = tdMatches[7].Groups[1].Value
                };
                coupons.Add(coupon);
            }
            return coupons;
        }

    }
}
